<?php

namespace app\api\controller;


use think\Cache;
use think\Log;

class Information extends ApiBase {
	public function _initialize() {
		$InfoTimeLine = Cache::get( 'InfoTimeLine' );
		if ( $InfoTimeLine ) {
			return;
		}
		$condition['is_top'] = 1;
		$condition['status'] = [ 'neq', config( 'code.status_delete' ) ];
		$infos               = model( 'Information' )
			->field( 'id,is_top,top_expert,status' )
			->where( $condition )
			->select();
		if ( $infos ) {
			foreach ( $infos as $info ) {
				if ( time() > $info->top_expert ) {
					$info->is_top = 0;
					$info->save();
				}
			}
		}
		Cache::set( 'InfoTimeLine', '2个小时', config( 'setting.token_expire_in' ) );
	}

	public function index() {
		$query                 = input( 'get.' );
		$condition['status']   = config( 'code.status_show' );
		$condition['need_pay'] = 0;
		if ( ! empty( $query['user'] ) && $query['user'] == 1 ) {
			$this->checkUserToken();
			$condition['status']  = [ 'neq', config( 'code.status_delete' ) ];
			$condition['user_id'] = [ 'eq', $this->user_id ];
		}
		if ( ! empty( $query['parent_id'] ) ) {
			$condition           = model( 'Category' )->getCatCondition( $query['parent_id'] );
			$condition['status'] = [ 'eq', config( 'code.status_show' ) ];
		}
		if ( ! empty( $query['start'] ) && ! empty( $query['end'] ) ) {
			$condition['status']      = [ 'neq', config( 'code.status_delete' ) ];
			$condition['create_time'] = [ 'between', [ $query['start'], $query['end'] ] ];
		}
		if ( ! empty( $query['keyword'] ) ) {
			$condition['content'] = [ 'like', '%' . $query['keyword'] . '%' ];
			$this->getPageAndSize( $query );
			$searchResult = model( 'Information' )->searchResult( $query['keyword'], $this->from, $this->size );

			return show( 0, 'OK', $searchResult );
		}
		if ( ! empty( $query['ids'] ) ) {
			$condition['id']     = [ 'in', $query['ids'] ];
			$condition['status'] = [ 'eq', config( 'code.status_show' ) ];
			$result              = model( "Information" )->getInformationByIds( $condition );

			return show( 0, 'ok', $result );
		}
		$this->getPageAndSize( $query );
		$result = model( 'Information' )->getInformation( $condition, $this->from, $this->size );

		return show( 0, 'OK', $result );
	}

	public function save() {
		$data            = input( 'post.' );
		$data['user_id'] = $this->user_id;
		$user            = model( 'User' )->get( [ 'id' => $this->user_id ] );
		if ( ! $user && $user->status == 0 ) {
			return show( 11, '用户没授权' );
		}
		if ( $user->status === 1 && $user->forbidden === 0 ) {
			$id = model( 'Information' )->add( $data );
			model( 'User' )->get( $this->user_id )->setInc( 'publish_count' );

			return show( 0, 'OK', [ 'id' => $id ] );
		} else {
			return show( 11, '你无权限发布' );
		}
	}

	public function read( $id ) {
		$condition['need_pay'] = 0;
		if ( input( 'get.type' ) === 'rec' ) {
			$condition['id']          = [ 'neq', input( 'get.exceptid' ) ];
			$condition['cat_id']      = $id;
			$condition['status']      = [ 'eq', config( 'code.status_show' ) ];
			$now                      = time();
			$thirtyDayAgo             = $now - ( 30 * 24 * 60 * 60 );
			$condition['create_time'] = [ 'between', [ $thirtyDayAgo, $now ] ];
			$order                    = [
				'create_time'   => 'desc',
				'share_count'   => 'desc',
				'collect_count' => 'desc',
				'read_count'    => 'desc'
			];
			$result                   = model( 'Information' )
				->getInformation( $condition, 0, config( 'setting.recommend_count' ), $order );
		} else {
			$condition['id']     = $id;
			$condition['status'] = [ 'neq', config( 'code.status_delete' ) ];
			$result              = model( 'Information' )->getInformation( $condition );
			model( 'Information' )->updateCount( $id, 'read_count' );
		}
		if ( ! $result ) {
			return show( 1, 'fail' );
		}

		return show( 0, 'OK', $result );
	}

	public function update( $id ) {
		if ( input( 'get.type' ) === 'share' ) {
			model( 'Information' )->updateCount( $id, 'share_count' );

			return show( config( 'code.success' ), 'OK' );
		}

		if ( input( 'get.type' ) === 'collect' ) {
			if ( input( 'get.isadd/d' ) == 1 ) {
				model( 'Information' )->updateCount( $id, 'collect_count' );
			} else {
				model( 'Information' )->updateCount( $id, 'collect_count', true );
			}

			return show( config( 'code.success' ), 'OK' );
		}
		if ( input( 'get.type' ) === 'repub' ) {
			model( 'Information' )->rePublishMessage( $id, $this->user_id );

			return show( config( 'code.success' ), '重新发布成功' );
		}
		if ( input( 'get.type' ) === 'changeStatus' ) {
			$status = input( 'put.status' );
			model( 'Information' )->changeStatus( $id, $this->user_id, $status );

			return show( config( 'code.success' ), '修改状态成功' );
		}

		return show( config( 'code.fail' ), '参数错误' );
	}

	public function delete( $id ) {
		$result = model( 'Information' )->where( [ 'id' => $id ] )->update( [ 'status' => config( 'code.status_delete' ) ] );
		if ( ! $result ) {
			return show( 1, '删除失败' );
		}

		return show( 0, 'delete success' );
	}
}